WSL2与Windows间的网络互访 您所在的位置:网站首页 linux 查看本地防火墙 WSL2与Windows间的网络互访

WSL2与Windows间的网络互访

2024-07-17 01:44| 来源: 网络整理| 查看: 265

我的WSL2使用的是ubuntu 20.04.2.LTS, Windows是10专业版 Build: 19043.1526。本文只讨论应用级别通过网络互相访问的问题,不涉及文件互访。

Windows的IP Address

由于多物理网卡,虚拟机网卡等原因,Windows会有多个IP Address,我们需要找出能与WSL2连同的那个。

启动WSL2,键入如下命令:

$cat /etc/resolv.conf

输出中nameserver 后面的地址即是我们要找的地址。

如:

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf: # [network] # generateResolvConf = false nameserver 172.27.160.1

172.27.160.1就是我们要找的Windows的IP Address,后面会以引用

WSL2的IP Address

启动WSL2,键入如下命令:

$ip a |grep "global eth0"

输出举例:

inet 172.27.170.146/20 brd 172.27.175.255 scope global eth0

172.27.170.146就是我们要找的WSL2的IP Address,后面会以引用

WSL2访问Windows

默认情况下Windows的防火墙会阻止WSL2中应用对Windows的网络访问(see: Add "allow" rule to Windows firewall for WSL2 network · Issue #4585 · microsoft/WSL (github.com)),解决办法是添加一条防火墙规则允许WSL2对Windows的访问。请以管理员身份打开PowerShell并键入以下命令:

PS C:\> New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow

输出举例:

Name : {273473a3-96c8-4b17-afc2-00231083cafe} DisplayName : WSL Description : DisplayGroup : Group : Enabled : True Profile : Any Platform : {} Direction : Inbound Action : Allow EdgeTraversalPolicy : Block LooseSourceMapping : False LocalOnlyMapping : False Owner : PrimaryStatus : OK Status : 已从存储区成功分析规则。(65536\) EnforcementStatus : NotApplicable PolicyStoreSource : PersistentStore PolicyStoreSourceType : Local

命令成功执行后,我们会在Windows的防火墙高级设置的入站规则里会看到一条名为WSL的新规则:

image.png

以上配置好后,我们就可以在WSL2中通过和访问Windows中的应用程序。

Windows访问WSL2

我们可以在Windows中通过和访问WSL2中的应用程序。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有